// 流式读取媒体
async function fetchStream() {
  // 当响应头传输完成返回resp
  const resp = await fetch(url);
  // 流式读取
  const reader = resp.body.getReader();
  // 创建解码器
  const textDecoder = new TextDecoder();
  while (1) {
    const { value, done } = await reader.read();
    // done为true表示已经传输完成
    if (done) {
      break;
    }
    // 将类型化数组（二进制数组）转为文本
    const str = textDecoder.decode(value);
    console.log(str);
  }
}

const btn = document.querySelector(".btn");
btn.onClick = async function (params) {
  const resp = await fetch(url, {
    headers: {
      authorization: `Bearer ${token}`,
    },
  });
  const blob = await resp.blob();
  const url = URL.createObjectURL(blob);
  const a = document.createElement("a");
  a.href = url;
  a.download = "es6.pdf";
  a.click();
};
